<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\middleware\Auth;
use think\facade\View;
use app\model\Region;
use app\model\Type;
use app\model\User as ModelUser;

class User extends BaseController
{
    protected $middleware = [
        Auth::class
    ];

    protected function initialize()
    {
        parent::initialize();

        $this->RegionModel = new Region();
        $this->TypeModel = new Type();
        $this->UserModel = new ModelUser();

    }

    public function index()
    {
        return View::fetch();
    }

    public function profile()
    {
        if($this->request->isPost())
        {
            // 接收所有的参数
            $params = $this->request->param();

            // 获取中间件传参
            $LoginUser = $this->UserModel->find(request()->LoginUser['id']);

            if(!$LoginUser)
            {
                cookie('LoginUser',null);
                $this->error('用户不存在，请先登录');
                halt();
            }

            $data = [
                'nickname' => $params['nickname'],
                'phone' => $params['phone'],
                'sex' => $params['sex'],
                'province' => $params['province'],
                'city' => $params['city'],
                'district' => $params['district']
            ];


            if($_FILES['avatar']['size'] > 0 && $_FILES['avatar'])
            {
                $avatar = upload('avatar');

                if($avatar['code'] === 0)
                {
                    $this->error($avatar['msg']);
                    halt();
                }

                $data['avatar'] = $avatar['data'];

            }

            if($params['password'])
            {
                $salt = build_ranstr();

                $password = md5($params['password'] . $salt);

                $data['password'] = $password;

                $data['salt'] = $salt;
            }


            $result = $this->UserModel->where(['id' => $LoginUser['id']])->save($data);

            if($result == FALSE)
            {
                @is_file($avatar['data'] ? $avatar['data'] : '') && @unlink($avatar['data']);
                $this->error('更新失败');
                halt();
            }else{
                $User = $this->UserModel->find(request()->LoginUser['id']);

                
                if($_FILES['avatar']['size'] > 0 && $_FILES['avatar'])
                {
                    @is_file($LoginUser['avatar'] ? $LoginUser['avatar'] : '') && @unlink($LoginUser['avatar']);
                }

                

                // 封装cookie数据
                $data = [
                    'id' => $User['id'],
                    'nickname' => $User['nickname'],
                    'email' => $User['email'],
                    'avatar' => $User['avatar'],
                    'sex' => $User['sex'],
                    'phone' => $User['phone']
                ];

                // 数组转json字符串
                $data = json_encode($data);

                // 设置session
                cookie('LoginUser',$data);

                $this->success('更新成功','/user/index');
                halt();
                
            }


        }

      

        // 查询用户的数据
        $User = $this->UserModel->find(request()->LoginUser['id']);

        // 查询省份地区的数据
        $provinces = $this->RegionModel->where(['grade' => 1])->select();

        if(empty($provinces))
        {
            $citys = [];
        }else{
            $citys = $this->RegionModel->where(['parentid' => $User['province']])->select();
        }

        if(empty($citys))
        {
            $districts = [];
        }else {
            $districts = $this->RegionModel->where(['parentid' => $User['city']])->select();
        }

        return View::fetch('',[
            'User' => $User,
            'provinces' => $provinces,
            'citys' => $citys,
            'districts' => $districts,
        ]);
    }
}
